﻿//	SCRIPT WRITTEN BY 
//	COPYRIGHT 2016 ©

using System;
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;

namespace Lost
{
	[RequireComponent(typeof (CharacterController))]
	[RequireComponent(typeof (AudioSource))]
	public class MyFirstPersonController : MonoBehaviour
	{
		[SerializeField] private bool m_IsWalking;
		[SerializeField] public float m_WalkSpeed;
		[SerializeField] public float m_RunSpeed;
		[SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
		[SerializeField] private float m_JumpSpeed;
		[SerializeField] private float m_StickToGroundForce;
		[SerializeField] private float m_GravityMultiplier;
		[SerializeField] private MyMouseLook m_MouseLook;
		[SerializeField] private bool m_UseFovKick;
		[SerializeField] private FOVKick m_FovKick = new FOVKick();
		[SerializeField] private bool m_UseHeadBob;
		[SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
		[SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
		[SerializeField] private float m_StepInterval;
		[SerializeField] public AudioClip[] m_FootstepSounds;    // an array of footstep sounds that will be randomly selected from.
		[SerializeField] public float footstep_volume;
		[SerializeField] public float jump_land_volume;
		[SerializeField] private AudioClip m_JumpSound;           // the sound played when character leaves the ground.
		[SerializeField] private AudioClip m_LandSound;           // the sound played when character touches back on ground.

		[SerializeField] float limitDepth = 1f;

		public bool isRunning;	//Are we running?
		public bool m_CanRun;	//Stamina
		private Camera m_Camera;
		private bool m_Jump;
		private float m_YRotation;
		private Vector2 m_Input;
		private Vector3 m_MoveDir = Vector3.zero;
		public CharacterController m_CharacterController;
		private CollisionFlags m_CollisionFlags;
		private bool m_PreviouslyGrounded;
		private Vector3 m_OriginalCameraPosition;
		private float m_StepCycle;
		private float m_NextStep;
		private bool m_Jumping;
		private AudioSource m_AudioSource;

		public bool CanRun
		{
			get { return m_CanRun; }
			set { m_CanRun = value; }
		}

		// Use this for initialization
		private void Start()
		{
			m_CharacterController = GetComponent<CharacterController>();
			m_Camera = Camera.main;
			m_OriginalCameraPosition = m_Camera.transform.localPosition;
			m_FovKick.Setup(m_Camera);
			m_HeadBob.Setup(m_Camera, m_StepInterval);
			m_StepCycle = 0f;
			m_NextStep = m_StepCycle/2f;
			m_Jumping = false;
			m_AudioSource = GetComponent<AudioSource>();
			m_MouseLook.Init(transform , m_Camera.transform);
		}


		// Update is called once per frame
		private void Update()
		{

			if (m_CharacterController.velocity.sqrMagnitude > 0 && Input.GetKey(KeyCode.LeftShift) && CanRun) {
				isRunning = true;
			}

			if (m_CharacterController.velocity.sqrMagnitude == 0)
			{
				isRunning = false;
			}

			if (m_CharacterController.velocity.sqrMagnitude > 0  && !Input.GetKey(KeyCode.LeftShift))
			{
				isRunning = false;
			}



			RotateView();
			// the jump state needs to read here to make sure it is not missed
			if (!m_Jump)
			{
				m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
			}

			if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
			{
				StartCoroutine(m_JumpBob.DoBobCycle());
				PlayLandingSound();
				m_MoveDir.y = 0f;
				m_Jumping = false;
			}
			if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
			{
				m_MoveDir.y = 0f;
			}

			m_PreviouslyGrounded = m_CharacterController.isGrounded;
		}


		private void PlayLandingSound()
		{
			m_AudioSource.clip = m_LandSound;
			m_AudioSource.volume = jump_land_volume;  
			m_AudioSource.Play();
			m_NextStep = m_StepCycle + .5f;
		}


		private void FixedUpdate()
		{
			float speed;
			GetInput(out speed);
			// always move along the camera forward as it is the direction that it being aimed at
			Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;

			// get a normal for the surface that is being touched to move along it
			RaycastHit hitInfo;
			Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
				m_CharacterController.height/2f, ~0, QueryTriggerInteraction.Ignore);
			desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;

			m_MoveDir.x = desiredMove.x*speed;
			m_MoveDir.z = desiredMove.z*speed;

			//=============限制玩家，不让玩家从高处掉下去========================================================
			if (!m_Jumping) {
				m_MoveDir = limitMove (m_MoveDir, limitDepth);
			}
			//=================================================================================================

			if (m_CharacterController.isGrounded)
			{
				m_MoveDir.y = -m_StickToGroundForce;

				if (m_Jump)
				{
					m_MoveDir.y = m_JumpSpeed;
					PlayJumpSound();
					m_Jump = false;
					m_Jumping = true;
				}
			}
			else
			{
				m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
			}
			m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);

			ProgressStepCycle(speed);
			UpdateCameraPosition(speed);

			m_MouseLook.UpdateCursorLock();
		}


		private void PlayJumpSound()
		{
			m_AudioSource.clip = m_JumpSound;
			m_AudioSource.volume = jump_land_volume;          
			m_AudioSource.Play();
		}


		private void ProgressStepCycle(float speed)
		{
			if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
			{
				m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
					Time.fixedDeltaTime;
			}

			if (!(m_StepCycle > m_NextStep))
			{
				return;
			}

			m_NextStep = m_StepCycle + m_StepInterval;

			PlayFootStepAudio();
		}


		private void PlayFootStepAudio()
		{
			if (!m_CharacterController.isGrounded)
			{
				return;
			}
			if (m_FootstepSounds == null || m_FootstepSounds.Length <= 0) {
				return;
			}
			// pick & play a random footstep sound from the array,
			// excluding sound at index 0
			int n = Random.Range(1, m_FootstepSounds.Length);
			m_AudioSource.clip = m_FootstepSounds[n];
			m_AudioSource.PlayOneShot(m_AudioSource.clip, footstep_volume);
			// move picked sound to index 0 so it's not picked next time
			m_FootstepSounds[n] = m_FootstepSounds[0];
			m_FootstepSounds[0] = m_AudioSource.clip;
		}


		private void UpdateCameraPosition(float speed)
		{
			Vector3 newCameraPosition;
			if (!m_UseHeadBob)
			{
				return;
			}
			if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
			{
				m_Camera.transform.localPosition =
					m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
						(speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
				newCameraPosition = m_Camera.transform.localPosition;
				newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
			}
			else
			{
				newCameraPosition = m_Camera.transform.localPosition;
				newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
			}
			m_Camera.transform.localPosition = newCameraPosition;
		}


		private void GetInput(out float speed)
		{
			bool waswalking = m_IsWalking;

			#if !MOBILE_INPUT
			// On standalone builds, walk/run speed is modified by a key press.
			// keep track of whether or not the character is walking or running
			m_IsWalking = !Input.GetKey(KeyCode.LeftShift);

			#endif
			// set the desired speed to be walking or running

			speed = m_IsWalking ? m_WalkSpeed : m_CanRun ? m_RunSpeed : m_WalkSpeed;
			m_Input = MyInput.GetPlayerWalk ();

			// normalize input if it exceeds 1 in combined length:
			if (m_Input.sqrMagnitude > 1)
			{
				m_Input.Normalize();
			}

			// handle speed change to give an fov kick
			// only if the player is going to a run, is running and the fovkick is to be used
			if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
			{				
				StopAllCoroutines();
				StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
			}
		}


		private void RotateView()
		{
			m_MouseLook.LookRotation (transform, m_Camera.transform);
		}


		private void OnControllerColliderHit(ControllerColliderHit hit)
		{
			Rigidbody body = hit.collider.attachedRigidbody;
			//dont move the rigidbody if the character is on top of it
			if (m_CollisionFlags == CollisionFlags.Below)
			{
				return;
			}

			if (body == null || body.isKinematic)
			{
				return;
			}
			body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
		}


		//=========================================================华丽的分割线=========================================================
		/// <summary>
		/// 输入想走的方向和限制的深度，输出被限制后的方向
		/// 检测的方向为想走的方向，以及左右偏移45°的两个方向
		/// </summary>
		/// <returns>The move.</returns>
		/// <param name="disireDir">Disire dir.</param>
		/// <param name="speed">Speed.</param>
		/// <param name="limitHeight">Limit height.</param>
		Vector3 limitMove(Vector3 disireDir, float limitDepth){
			Vector3 regionY = new Vector3 (0f, disireDir.y, 0f);
			disireDir.y = 0f;
			float offsetDis = Time.fixedDeltaTime * 10f;
			Vector3 checkPos = transform.position;
			Vector3 checkDir = new Vector3 (0f, -1f, 0f);
			float checkDis = 10f;
			float curHeight = checkDis;
			Ray ray = new Ray (checkPos, checkDir);
			RaycastHit hit;
			if (Physics.Raycast (ray, out hit, checkDis)) {
				//Gizmos.DrawLine (ray.origin, hit.point);
				curHeight = hit.distance;
			}

			ray = new Ray (checkPos + disireDir * offsetDis, checkDir);
			if (Physics.Raycast (ray, out hit, checkDis) && hit.distance <= curHeight + limitDepth) {
				//Gizmos.DrawLine (ray.origin, hit.point);
				return disireDir + regionY;
			}

			offsetDis /= 1.414f;
			Vector3 verticleDir = new Vector3 (disireDir.z, 0f, -disireDir.x);
			ray = new Ray (checkPos + (disireDir + verticleDir) * offsetDis, checkDir);
			if (Physics.Raycast (ray, out hit, checkDis) && hit.distance <= curHeight + limitDepth) {
				//Gizmos.DrawLine (ray.origin, hit.point);
				return (disireDir + verticleDir) / 1.414f + regionY;
			}
			ray = new Ray (checkPos + (disireDir - verticleDir) * offsetDis, checkDir);
			if (Physics.Raycast (ray, out hit, checkDis) && hit.distance <= curHeight + limitDepth) {
				//Gizmos.DrawLine (ray.origin, hit.point);
				return (disireDir - verticleDir) / 1.414f + regionY;
			}

			return Vector3.zero;
		}
			
		[SerializeField] float MaxDepth = 10f;
		Transform preStandingObj;
		Vector3 regionPosOfObjStanding;
		/// <summary>
		/// 当玩家站在一个移动的物体上时，让玩家跟随该物体移动
		/// </summary>
		void FollowObjStanding(){
			Ray ray = new Ray (transform.position, -transform.up);
			RaycastHit hit;
			if (Physics.SphereCast (ray, m_CharacterController.radius, out hit, MaxDepth)) {
				if (preStandingObj != hit.transform || regionPosOfObjStanding == null) {
					//当站住了才可以赋值，接着跟随移动
					if (!m_Jumping) {
						preStandingObj = hit.transform;
						regionPosOfObjStanding = hit.transform.position;
					}
					return;
				} else {
					transform.position += hit.transform.position - regionPosOfObjStanding;
					preStandingObj = hit.transform;
					regionPosOfObjStanding = hit.transform.position;
				}
			}// else if (preStandingObj != null && regionPosOfObjStanding != null) {
			//	transform.Translate (preStandingObj.position - regionPosOfObjStanding);
			//}
			return;
		}

		void LateUpdate(){

			//=================================================================================================
			FollowObjStanding ();
			//=================================================================================================
		}

	}
}
